home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gnat1792.zip / gnat179b / t-adainc / s-taskin.adb < prev    next >
Text File  |  1994-05-19  |  7KB  |  258 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                 GNU ADA RUNTIME LIBRARY (GNARL) COMPONENTS               --
  4. --                                                                          --
  5. --                        S Y S T E M . T A S K I N G                       --
  6. --                                                                          --
  7. --                                  B o d y                                 --
  8. --                                                                          --
  9. --                             $Revision: 1.2 $                             --
  10. --                                                                          --
  11. --           Copyright (c) 1991,1992,1993, FSU, All Rights Reserved         --
  12. --                                                                          --
  13. --  GNARL is free software; you can redistribute it and/or modify it  under --
  14. --  terms  of  the  GNU  Library General Public License as published by the --
  15. --  Free Software Foundation; either version 2,  or (at  your  option)  any --
  16. --  later  version.   GNARL is distributed in the hope that it will be use- --
  17. --  ful, but but WITHOUT ANY WARRANTY; without even the implied warranty of --
  18. --  MERCHANTABILITY  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Gen- --
  19. --  eral Library Public License for more details.  You should have received --
  20. --  a  copy of the GNU Library General Public License along with GNARL; see --
  21. --  file COPYING. If not, write to the Free Software Foundation,  675  Mass --
  22. --  Ave, Cambridge, MA 02139, USA.                                          --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. --  This package body has to be eliminated once the offset calulation for
  27. --  ATCB is done statically. Also, the temporary placement of queuing
  28. --  primitives has to move back to Tasking.Queuing. (compiler error) ???
  29.  
  30. with System.Task_Primitives;
  31. --  Used for,  Task_Primitives.TCB_Ptr,
  32. --             Task_Primitives.Self
  33.  
  34. with System.Storage_Elements;
  35. --  Used for,  Storage_Elements.Storage_Offset,
  36. --             Storage_Elements."-"
  37. --             Storage_Elements.Storage_Count
  38.  
  39. with System.Tasking.Runtime_Types;
  40. --  Used for,  Runtime_Types.Ada_Task_Control_Block;
  41.  
  42. with Unchecked_Conversion;
  43.  
  44. package body System.Tasking is
  45.  
  46.    function "-"
  47.      (A    : System.Address;
  48.       B    : System.Address)
  49.       return Storage_Elements.Storage_Offset
  50.    renames Storage_Elements."-";
  51.  
  52.    function "-"
  53.      (A    : System.Address;
  54.       I    : Storage_Elements.Storage_Offset)
  55.       return System.Address
  56.    renames Storage_Elements."-";
  57.  
  58.    function Get_LL_TCB_Offset return Storage_Elements.Storage_Count;
  59.  
  60.    LL_TCB_Offset : Storage_Elements.Storage_Count := Get_LL_TCB_Offset;
  61.  
  62.    function Address_To_Task_ID is new
  63.      Unchecked_Conversion (System.Address, Task_ID);
  64.  
  65.    function TCB_Ptr_To_Address is new
  66.      Unchecked_Conversion (Task_Primitives.TCB_Ptr, System.Address);
  67.  
  68.    -----------------------
  69.    -- Get_LL_TCB_Offset --
  70.    -----------------------
  71.  
  72.    function Get_LL_TCB_Offset return Storage_Elements.Storage_Count is
  73.       ATCB_Record : Runtime_Types.Ada_Task_Control_Block (0);
  74.  
  75.    begin
  76.       return ATCB_Record.LL_TCB'Address - ATCB_Record'Address;
  77.    end Get_LL_TCB_Offset;
  78.  
  79.    ----------
  80.    -- Self --
  81.    ----------
  82.  
  83.    --  This is an INLINE_ONLY version of Self for use in the RTS.
  84.  
  85.    function Self return Task_ID is
  86.       S : Task_Primitives.TCB_Ptr := Task_Primitives.Self;
  87.  
  88.    begin
  89.       return Address_To_Task_ID (TCB_Ptr_To_Address (S) - LL_TCB_Offset);
  90.    end Self;
  91.  
  92.  
  93.    --  The following functions are in Tasking.Queuing.
  94.    --  However, because of the compiler intyernal error,
  95.    --  They are temporarily moved to here. ???
  96.  
  97.    -------------
  98.    -- Enqueue --
  99.    -------------
  100.  
  101.    --  Enqueue call at the end of entry_queue E
  102.  
  103.    procedure Enqueue (E : in out Entry_Queue; Call : Entry_Call_Link) is
  104.    begin
  105.       if E.Head = null then
  106.          E.Head := Call;  --  E.Tail should also be null here
  107.       else
  108.          E.Tail.Next := Call;
  109.       end if;
  110.  
  111.       E.Tail := Call;
  112.       Call.Next := E.Head; --  make circular linked-list
  113.    end Enqueue;
  114.  
  115.    -------------
  116.    -- Dequeue --
  117.    -------------
  118.  
  119.    --  Dequeue call from entry_queue E
  120.  
  121.    procedure Dequeue (E : in out Entry_Queue; Call : Entry_Call_Link) is
  122.       Prev : Entry_Call_Link;
  123.  
  124.    begin
  125.       --  If empty queue, simply return
  126.  
  127.       if E.Head = null then
  128.          return;
  129.       end if;
  130.  
  131.       if E.Head = Call then
  132.          if E.Tail = Call then
  133.             E.Head := null; --  case of one element
  134.             E.Tail := null;
  135.          else
  136.             E.Head := Call.Next;
  137.             E.Tail.Next := E.Head;
  138.          end if;
  139.  
  140.          --  Successfully dequeued
  141.  
  142.          Call.Next := null;
  143.  
  144.       else
  145.          --  At this point we know that the queue has more than one element
  146.  
  147.          Prev := E.Head;
  148.          loop
  149.             if Prev.Next = Call then
  150.                Prev.Next := Call.Next;
  151.  
  152.                if E.Tail = Call then
  153.                   E.Tail := Prev;
  154.                end if;
  155.  
  156.                --  Successfully dequeued
  157.  
  158.                Call.Next := null;
  159.                exit;
  160.             end if;
  161.  
  162.             --  Exit if call is not found
  163.  
  164.             exit when Prev.Next = E.Tail;
  165.             Prev := Prev.Next;
  166.          end loop;
  167.       end if;
  168.    end Dequeue;
  169.  
  170.    ----------
  171.    -- Head --
  172.    ----------
  173.  
  174.    --  Return the head of entry_queue E
  175.  
  176.    function Head (E : in Entry_Queue) return Entry_Call_Link is
  177.    begin
  178.       return E.Head;
  179.    end Head;
  180.  
  181.    ------------------
  182.    -- Dequeue_Head --
  183.    ------------------
  184.  
  185.    --  Remove and return the head of entry_queue E
  186.  
  187.    procedure Dequeue_Head
  188.      (E    : in out Entry_Queue;
  189.       Call : out Entry_Call_Link)
  190.    is
  191.       Temp : Entry_Call_Link;
  192.  
  193.    begin
  194.       --  If empty queue, return null pointer
  195.  
  196.       if E.Head = null then
  197.          Call := null;
  198.          return;
  199.       end if;
  200.  
  201.       Temp := E.Head;
  202.  
  203.       if E.Head = E.Tail then
  204.          E.Head := null; --  case of one element
  205.          E.Tail := null;
  206.       else
  207.          E.Head := Temp.Next;
  208.          E.Tail.Next := E.Head;
  209.       end if;
  210.  
  211.       --  Successfully dequeued
  212.  
  213.       Temp.Next := null;
  214.       Call := Temp;
  215.    end Dequeue_Head;
  216.  
  217.    -------------
  218.    -- Onqueue --
  219.    -------------
  220.  
  221.    --  Return True if Call is on any entry_queue at all
  222.  
  223.    function Onqueue (Call : Entry_Call_Link) return Boolean is
  224.    begin
  225.       --  Utilize the fact that every queue is circular, so if Call
  226.       --  is on any queue at all, Call.Next must NOT be null.
  227.  
  228.       return Call.Next /= null;
  229.    end Onqueue;
  230.  
  231.    -------------------
  232.    -- Count_Waiting --
  233.    -------------------
  234.  
  235.    --  Return number of calls on the waiting queue of E
  236.  
  237.    function Count_Waiting (E : in Entry_Queue) return Natural is
  238.       Count : Natural;
  239.       Temp : Entry_Call_Link;
  240.  
  241.    begin
  242.       Count := 0;
  243.  
  244.       if E.Head /= null then
  245.          Temp := E.Head;
  246.  
  247.          loop
  248.             Count := Count + 1;
  249.             exit when E.Tail = Temp;
  250.             Temp := Temp.Next;
  251.          end loop;
  252.       end if;
  253.  
  254.       return Count;
  255.    end Count_Waiting;
  256.  
  257. end System.Tasking;
  258.